﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KnowlegeAssistant
{
    public partial class EditTheme : Form
    {

        public enum ActionType { add, edit }

        private ActionType action_type;
        private Theme theme;

        public EditTheme(Theme theme, ActionType action_type)
        {
            InitializeComponent();
            this.action_type = action_type;
            this.theme = theme;
            this.textBox1.Text = theme.Name;
            InitControlNames();
            RefreshData();
            this.subjectsComboBox.SelectedItem = theme.Subject;
        }

        public EditTheme(ActionType action_type)
        {
            InitializeComponent();
            this.action_type = action_type;
            InitControlNames();            
            RefreshData();
        }

        public EditTheme(ActionType action_type, Subject subject)
        {
            InitializeComponent();
            this.action_type = action_type;
            InitControlNames();
            RefreshData();
            foreach (Subject s in subjectsComboBox.Items)
            {
                if (s.Name == subject.Name)
                {
                    this.subjectsComboBox.SelectedItem = s;
                }
            }
        }

        private void EditTheme_Load(object sender, EventArgs e)
        {

        }

        private void InitControlNames()
        {
            if (this.action_type == ActionType.add)
            {
                this.Text = "AddTheme";
                this.button1.Text = "Добавить";
            }
            else if (this.action_type == ActionType.edit)
            {
                this.Text = "EditTheme";
                this.button1.Text = "Применить";
            }
        }

        private void RefreshData()
        {
            //Subjects,Themes,Tests
            subjectsComboBox.DataSource = SubjectUtil.GetAllSubjects();
            subjectsComboBox.DisplayMember = "Name";
            if (subjectsComboBox.Items.Count != 0)
            {
                subjectsComboBox.SelectedIndex = 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string theme_name = this.textBox1.Text.Trim();
            Subject subject = (Subject)this.subjectsComboBox.SelectedItem;
            if (this.action_type == ActionType.add)
            {
                ThemeUtil.AddTheme(theme_name, subject);
            }
            else if (this.action_type == ActionType.edit)
            {
                //ThemeUtil.EditTheme(theme.ThemeID,theme_name, subject);
            }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
